<template>
  <DataList
    ref="datalist"
    :cate="cate"
    :cols="cols"
    :name="name"
    :excelHeader="excelHeader"
    skipExcelNumber="2"
  >
  <template slot="search">
      <el-form-item>
        <el-button
          type="primary"
          @click="flush"
        >自动在校生汇总</el-button>
      </el-form-item>
    </template>
  </DataList>
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import linq from "linq";
import { setSaveAfter2019 } from "@/api/案例分析.js";
import { showLoading, hideLoading } from "@/utils/loading"
import { findComponent } from "@/utils/ui";
import {
  get开设专业,
  update开设专业,
  delete开设专业,
  add开设专业,
  bulkAdd开设专业
} from "@/api/开设专业.js";
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: "A7-1-1开设专业表",
      name: "7.1.1 开设专业",
      search_str: "",
      excelHeader: {
        B3: "系部代码",
        C3: "所属系部",
        D3: "2020年是否招生（含方向）",
        E3: "专业代码",
        F3: "专业名称（全称）",
        G3: "专业方向代码",
        H3: "专业方向名称（全称）",
        I3: "批准设置日期（年月）",
        J3: "首次招生日期（年月）",
        K3: "修业年限（年）",
        L3: "重点专业",
        M3: "特色专业",
        N3: "是否现代学徒制试点专业",
        O3: "是否国际合作专业",
        P3: "是否有上届毕业生"
      },
      cols: [
         { label: "所属系部代码", prop: "BMBH" },
        { label: "专业所属系部", prop: "BMMC" },
        { label: "2020年是否招生(含方向)", prop: "XZ_SFZS" },
        { label: "专业代码", prop: "ZYDM" },
        { label: "专业名称(全称)", prop: "ZYMC" },
        { label: "专业方向代码", prop: "ZYFXDM" },
        { label: "专业方向名称(全称)", prop: "ZYFXMC" },
        { label: "批准设置日期(年月)", prop: "JLNY" },
        { label: "首次招生日期(年月)", prop: "QSXQ" },
        { label: "修业年限(年)", prop: "XZ" },
        {
          label: "在校生数(人)",
          data: [
            { label: "总人数", prop: "ZXSS" },
            { label: "一年级", prop: "YNJZXS" },
            { label: "二年级", prop: "ENJZXS" },
            { label: "三年级", prop: "SNJZXS" }
          ]
        },

        {
          label: "生源类型(人)",
          data: [
            { label: "普通高中起点", prop: "SYJG_PTGZ" },
            {
              label: "中职起点",
              data: [
                { label: "合计", prop: "SYJG_ZZQD_HJ" },
                { label: "其中:五年制后二年", prop: "SYJG_ZZQD_HEN" }
              ]
            },
            { label: "其他", prop: "SYJG_QT" }
          ]
        },
        { label: "重点专业", prop: "XZ_ZDZY" },
        { label: "特色专业", prop: "XZ_TSZY" },
        { label: "是否现代学徒制试点专业", prop: "XZ_SFXTZ" },
        { label: "是否国际合作专业", prop: "XZ_SFGJHZZY" },
        { label: "班级总数(个)", prop: "BJZS" },
        {
          label: "订单(定向)培养情况",
          data: [
            { label: "班级数(个)", prop: "DDPYBJS" },
            { label: "学生数(个)", prop: "DDPYXSS" }
          ]
        },
        { label: "是否有上届毕业生", prop: "XZ_SFYSJBYS" }
      ]
    };
  },
  methods: {
    getSummaries(list) {
      //
      const sums = [];
      this.cols.forEach((column, index) => {
        sums[index] = "";
      });
      sums[0] = "合计";
      sums[6] =
        "系部数:" +
        linq
          .from(list)
          .distinct("$.BMBH")
          .count();
      sums[7] = "招生专业:" + linq.from(list).sum("$.XZ_SFZS == '是'");
      sums[8] =
        "专业数:" +
        linq
          .from(list)
          .distinct("$.ZYDM")
          .count();
      sums[10] =
        "方向数:" +
        linq
          .from(list)
          .distinct("$.ZYFXDM")
          .count();
      sums[15] = "总人数:" + linq.from(list).sum("$.ZXSS");
      sums[16] = "一年级:" + linq.from(list).sum("$.YNJZXS");
      sums[17] = "二年级:" + linq.from(list).sum("$.ENJZXS");
      sums[18] = "三年级:" + linq.from(list).sum("$.SNJZXS");
      sums[19] = "普通高中起点:" + linq.from(list).sum("$.SYJG_PTGZ");
      sums[20] = "合计:" + linq.from(list).sum("$.SYJG_ZZQD_HJ");
      sums[21] = "五年制后二年:" + linq.from(list).sum("$.SYJG_ZZQD_HEN");
      sums[22] = "其他:" + linq.from(list).sum("$.SYJG_QT");
      sums[23] =
        "省级以上" +
        linq
          .from(list)
          .where("$.XZ_ZDZY=='省级'||$.XZ_ZDZY=='国家级'")
          .count();
      sums[24] =
        "省级以上" +
        linq
          .from(list)
          .where("$.XZ_TSZY=='省级'||$.XZ_TSZY=='国家级'")
          .count();
      sums[25] =
        "现代学徒制试点专业:" + linq.from(list).sum("$.XZ_SFXTZ == '是'");
      sums[26] = "国际合作专业:" + linq.from(list).sum("$.XZ_SFGJHZZY == '是'");
      sums[27] = "班级总数:" + linq.from(list).sum("$.BJZS");
      sums[28] = "班级数:" + linq.from(list).sum("$.DDPYBJS");
      sums[29] = "学生数:" + linq.from(list).sum("$.DDPYXSS");
      sums[30] = "有上届毕业生:" + linq.from(list).sum("$.XZ_SFYSJBYS == '是'");
      sums[31] =
        "通过:" +
        linq
          .from(list)
          .where("$.审核状态=='通过'")
          .count();
      sums[31] +=
        ",不通过:" +
        linq
          .from(list)
          .where("$.审核状态=='不通过'")
          .count();
      sums[31] +=
        ",待审核:" +
        linq
          .from(list)
          .where("$.审核状态=='待审核'")
          .count();

      return sums;
      //
    },
    async flush() {
      try {
        showLoading("正在汇总");
       
        await setSaveAfter2019(this.$root.user.XXID, this.$root.user.CJPC);
        hideLoading();
      } catch (e) {
        this.$alert(e);
      }
    },
    onDlgCreated(model) {
      var schema = require("../../../../../static/profession.json");
      var mc = findComponent(this, "ZYMC");
      var cc = schema.map(item => {
        return { label: item.专业名称, value: item.专业名称 };
      });
      mc.$attrs.items = cc;
      var dd = findComponent(this, "BMMC");
      var 专业 = linq
        .from(this.$root.机构设置)
        .distinct("$.BMMC")
        .toArray();
      dd.$attrs.items = 专业.map(item => {
        return { label: item.BMMC, value: item.BMMC };
      });
    },
    async onChange(model, field, value) {
      //
       if (field == "BMMC") {
          var s = linq.from(this.$root.机构设置).firstOrDefault(x=>x.BMMC == value);
          model.BMBH= s.BMBH;
      }
      if (field == "ZYMC") {
        var schema = require("../../../../../static/profession.json");

        var item = schema.find(item => {
          return item.专业名称 == value;
        });
        model.ZYDM = item.专业代码;
        model.ZYFXDM = item.专业方向代码;
        model.ZYFXMC = item.专业名称;
      }
      //
      model.ZXSS =
        Number(model.YNJZXS) + Number(model.ENJZXS) + Number(model.SNJZXS);
    },
    add_check(model, list) {
      //
      //是否已经存在数据
      var first = linq.from(list).firstOrDefault(e => e.ZYFXDM == model.ZYFXDM);
      if (first) return "已经存在对应编号的专业方向代码";
      //
    },
    check(array, list) {
      return new Promise(async (resolve, reject) => {
        try {
          var duplicates = linq
            .from(array)
            .where("!$.error")
            .groupBy("$.BMMC+','+$.ZYDM+','+$.ZYFXDM")
            .where("$.count()>1")
            .toArray();
          duplicates.forEach(e => {
            linq
              .from(array)
              .where(t => t.BMMC + "," + t.ZYDM + "," + t.ZYFXDM == e.key())
              .forEach(
                t =>
                  (t.error =
                    "录入数据存在相同的系部名称,专业方向代码与专业代码组合")
              );
          });
          //是否已经存在数据
          var err = linq
            .from(array)
            .where("!$.error")
            .select("$.BMMC+','+$.ZYDM+','+$.ZYFXDM")
            .intersect(
              linq
                .from(list)
                .select("$.BMMC+','+$.ZYDM+','+$.ZYFXDM")
                .toArray()
            )
            .toArray();

          linq
            .from(array)
            .where(
              e => err.indexOf(e.BMMC + "," + e.ZYDM + "," + e.ZYFXDM) !== -1
            )
            .forEach(
              e =>
                (e.error = e.error
                  ? e.error + ";"
                  : "" +
                    "数据库中已经存在相同的系部名称,专业方向代码与专业代码组合")
            );

          resolve();
        } catch (e) {
          reject(e);
        }
      });
    },

    async fetchData() {
      var user = await getUser();
      var datas = await get开设专业(user.XXID, user.CJPC);
      return datas;
    },
    async bulkAdd(array) {
      await bulkAdd开设专业({ Kszy: array });
    },

    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        console.log(JSON.stringify(temp));
        var model = await add开设专业(temp);
        resolve(model);
      });
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        console.log(JSON.stringify(temp));
        var model = await update开设专业(temp);
        resolve(model);
      });
      //
    },
    ondelete(model) {
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        await delete开设专业(temp);
        resolve();
      });
    }
  }
};
</script>
<style>
</style>